import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:tender_flutter/constant/theme_colors.dart';
import 'package:tender_flutter/page/home/model/city_entity.dart';
import 'package:tender_flutter/utils/screen_adaper.dart';

/// 选中城市的回调
///  levelIndex 层级
///  itemIndex item索引
///  cityModel 选中的数据
typedef SelectDropMenuCityCallback(
    int levelIndex, int itemIndex, CityEntity cityModel);

class DropMenuAddressWidget extends StatefulWidget {
  /// 城市数据
  final List<CityEntity> cityDatas;

  /// 选择之后的回调
  final SelectDropMenuCityCallback selectCityCallback;

  const DropMenuAddressWidget(
      {Key key, this.cityDatas, this.selectCityCallback})
      : super(key: key);

  @override
  _DropMenuAddressWidgetState createState() => _DropMenuAddressWidgetState();
}

// ignore: must_be_immutable
class _DropMenuAddressWidgetState extends State<DropMenuAddressWidget> {
  int _selectTempFirstLevelIndex = 0;
  int _selectFirstLevelIndex = 0;
  int _selectSecondLevelIndex = -1;

  /// 行高
  final double cellHeight = ScreenAdapter.height(90);

  @override
  Widget build(BuildContext context) {
    return _buildAddressWidget();
  }

  _buildAddressWidget() {
    return IntrinsicHeight(
      child: Row(
        children: <Widget>[
          Container(
            width: ScreenAdapter.width(300),
            child: ListView(
              children: widget.cityDatas.map((model) {
                int index = widget.cityDatas.indexOf(model);
                return InkWell(
                  onTap: () {
                    _selectTempFirstLevelIndex = index;
                    widget.selectCityCallback(0, index, model);
                  },
                  child: Container(
                      height: cellHeight,
                      color: _selectTempFirstLevelIndex == index
                          ? ThemeColors.colorType13
                          : ThemeColors.colorWhite,
                      alignment: Alignment.center,
                      child: _selectTempFirstLevelIndex == index
                          ? Text(
                              model.title,
                              style: TextStyle(
                                  color: ThemeColors.colorType1,
                                  fontSize: ScreenAdapter.sp(30)),
                            )
                          : Text(model.title)),
                );
              }).toList(),
            ),
          ),
          Expanded(
            child: Container(
              color: ThemeColors.colorType13,
              child:
                  // _selectTempFirstLevelIndex == 0 ? Container() :
                  ListView(
                children: widget.cityDatas[_selectTempFirstLevelIndex].areas
                    .map((model) {
                  int index = widget.cityDatas[_selectTempFirstLevelIndex].areas
                      .indexOf(model);
                  return InkWell(
                      onTap: () {
                        _selectSecondLevelIndex = index;
                        _selectFirstLevelIndex = _selectTempFirstLevelIndex;
                        if (_selectSecondLevelIndex == 0) {
                          widget.selectCityCallback(1, index,
                              widget.cityDatas[_selectFirstLevelIndex]);
                        } else {
                          widget.selectCityCallback(1, index, model);
                        }
                      },
                      child: Container(
                        height: cellHeight,
                        alignment: Alignment.centerLeft,
                        child: Row(children: <Widget>[
                          SizedBox(
                            width: 20,
                          ),
                          _selectFirstLevelIndex ==
                                      _selectTempFirstLevelIndex &&
                                  _selectSecondLevelIndex == index
                              ? Text(
                                  model.title,
                                  style: TextStyle(
                                      color: ThemeColors.colorType1,
                                      fontSize: ScreenAdapter.sp(30)),
                                )
                              : Text(model.title),
                        ]),
                      ));
                }).toList(),
              ),
            ),
          )
        ],
      ),
    );
  }
}
